home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / libcairo-perl / examples / png / text.pl < prev   
Encoding:
Perl Script  |  2006-05-21  |  3.1 KB  |  151 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Cairo;
  6.  
  7. use constant
  8. {
  9.     WIDTH => 450,
  10.     HEIGHT => 600,
  11.     TEXT => 'hello, world',
  12.     NUM_GLYPHS => 10,
  13.     M_PI => 4 * atan2(1, 1),
  14. };
  15.  
  16. sub box_text
  17. {
  18.     my ($cr, $utf8, $x, $y) = @_;
  19.  
  20.     $cr->save;
  21.  
  22.     my $extents = $cr->text_extents (TEXT);
  23.     my $line_width = $cr->get_line_width;
  24.     $cr->rectangle ($x + $extents->{x_bearing} - $line_width,
  25.                     $y + $extents->{y_bearing} - $line_width,
  26.                     $extents->{width} + 2 * $line_width,
  27.                     $extents->{height} + 2 *$line_width);
  28.     $cr->stroke;
  29.  
  30.     $cr->move_to ($x, $y);
  31.     $cr->show_text ($utf8);
  32.     $cr->move_to ($x, $y);
  33.     $cr->text_path ($utf8);
  34.     $cr->set_source_rgb (1, 0, 0);
  35.     $cr->set_line_width (1.0);
  36.     $cr->stroke;
  37.  
  38.     $cr->restore;
  39. }
  40.  
  41. sub box_glyphs
  42. {
  43.     my ($cr, $x, $y, @glyphs) = @_;
  44.  
  45.     $cr->save;
  46.  
  47.     my $extents = $cr->glyph_extents (@glyphs);
  48.     my $line_width = $cr->get_line_width;
  49.     $cr->rectangle ($x + $extents->{x_bearing} - $line_width,
  50.                     $y + $extents->{y_bearing} - $line_width,
  51.                     $extents->{width} + 2 * $line_width,
  52.                     $extents->{height} + 2 * $line_width);
  53.     $cr->stroke;
  54.  
  55.     foreach my $glyph (@glyphs) {
  56.         $glyph->{x} += $x;
  57.         $glyph->{y} += $y;
  58.     }
  59.     $cr->show_glyphs (@glyphs);
  60.     $cr->glyph_path (@glyphs);
  61.     $cr->set_source_rgb (1, 0, 0);
  62.     $cr->set_line_width (1.0);
  63.     $cr->stroke;
  64.     foreach my $glyph (@glyphs) {
  65.         $glyph->{x} -= $x;
  66.         $glyph->{y} -= $y;
  67.     }
  68.  
  69.     $cr->restore;
  70. }
  71.  
  72. {
  73.     my $surface = Cairo::ImageSurface->create ('argb32', WIDTH, HEIGHT);
  74.     my $cr = Cairo::Context->create ($surface);
  75.  
  76.     $cr->set_source_rgb (0, 0, 0);
  77.     $cr->set_line_width (2.0);
  78.  
  79.     $cr->save;
  80.     $cr->rectangle (0, 0, WIDTH, HEIGHT);
  81.     $cr->set_source_rgba (0, 0, 0, 0);
  82.     $cr->set_operator ('source');
  83.     $cr->fill;
  84.     $cr->restore;
  85.  
  86.     $cr->select_font_face ('sans', 'normal', 'normal');
  87.     $cr->set_font_size (40);
  88.     if (1) {
  89.         my $matrix = Cairo::Matrix->init_scale (40, -40);
  90.         $cr->set_font_matrix ($matrix);
  91.  
  92.         $cr->scale (1, -1);
  93.         $cr->translate (0, - HEIGHT);
  94.     }
  95.  
  96.     my $font_extents = $cr->font_extents;
  97.     my $height = $font_extents->{height};
  98.  
  99.     my @glyphs = ();
  100.     my $dx = 0;
  101.     my $dy = 0;
  102.     foreach (0 .. NUM_GLYPHS - 1) {
  103.         my $glyph = { index => $_ + 4, x => $dx, y => $dy };
  104.         my $extents = $cr->glyph_extents ($glyph);
  105.         $dx += $extents->{x_advance};
  106.         $dy += $extents->{y_advance};
  107.         push @glyphs, $glyph;
  108.     }
  109.  
  110.     box_text ($cr, TEXT, 10, $height);
  111.  
  112.     $cr->translate (0, $height);
  113.     $cr->save;
  114.     {
  115.         $cr->translate (10, $height);
  116.         $cr->rotate (10 * M_PI / 180);
  117.         box_text ($cr, TEXT, 0, 0);
  118.     }
  119.     $cr->restore;
  120.  
  121.     $cr->translate (0, 2 * $height);
  122.     $cr->save;
  123.     {
  124.         my $matrix = Cairo::Matrix->init_identity;
  125.         $matrix->scale (40, -40);
  126.         $matrix->rotate (-10 * M_PI / 180);
  127.         $cr->set_font_matrix ($matrix);
  128.         box_text ($cr, TEXT, 10, $height);
  129.     }
  130.     $cr->restore;
  131.  
  132.     $cr->translate (0, 2 * $height);
  133.     box_glyphs ($cr, 10, $height, @glyphs);
  134.  
  135.     $cr->translate (10, 2 * $height);
  136.     $cr->save;
  137.     {
  138.         $cr->rotate (10 * M_PI / 180);
  139.         box_glyphs ($cr, 0, 0, @glyphs);
  140.     }
  141.     $cr->restore;
  142.  
  143.     $cr->translate (0, $height);
  144.     foreach (0 .. NUM_GLYPHS - 1) {
  145.         $glyphs[$_]->{y} += $_ * 5;
  146.     }
  147.     box_glyphs ($cr, 10, $height, @glyphs);
  148.  
  149.     $surface->write_to_png ('text.png');
  150. }
  151.